//
//  MoreWorkmateDecryptVC.m
//  MyProject
//
//  Created by 石小强 on 2018/3/23.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "MoreWorkmateDecryptVC.h"

@interface MoreWorkmateDecryptVC ()

@property (nonatomic, strong) NSMutableArray *decrypArray;

@end

@implementation MoreWorkmateDecryptVC {
    NSInteger _pageNo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"更多同事解密";
    
    self.tableView.estimatedRowHeight = 136.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MoreWorkmateDecryptCell class]) bundle:nil] forCellReuseIdentifier:@"MoreWorkmateDecryptCell"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [self loadDecryptData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageNo += 1;
        [self loadDecryptData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

// 获取同事解密数据
- (void)loadDecryptData {
    WS(weakSelf)
    [SendRequest getIncumbentDecryptByCompanyId:self.companyId andPageNo:[NSString stringWithFormat:@"%zd", _pageNo] result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"获取同事解密数据 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [DecryptModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            if (_pageNo == 1) {
                [weakSelf.decrypArray removeAllObjects];
                [weakSelf.decrypArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
            } else {
                [weakSelf.decrypArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                if (datas.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
            }
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.decrypArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MoreWorkmateDecryptCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreWorkmateDecryptCell"];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
//    if (indexPath.row % 2 == 0) {
//        cell.nameLabel.text = @"如何通过苹果公司的UED设计师面试";
//    } else {
//        cell.nameLabel.text = @"如何通过苹果公司的UED设计师面试如何通过苹果公司的UED设计师面试如何通过苹果公司的UED设计师面试如何通过苹果公司的UED设计师面试";
//    }
    cell.model = [self.decrypArray objectAtIndex:indexPath.row];
    return cell;
}

- (NSMutableArray *)decrypArray {
    if (!_decrypArray) {
        _decrypArray = [[NSMutableArray alloc] init];
    }
    return _decrypArray;
}

@end
